/*
 * JuridicalPersonService.java
 * 
 * Created: Jul 6, 2015
 * 
 * Copyright (c) ExpertPB 2015
 * All information contained herein is, and remains the property of
 * ExpertPB and its suppliers, if any.
 */
package com.emex.app.web.service;

import com.emex.app.shared.data.JuridicalPerson;
import com.emex.app.shared.data.JuridicalPersonExample;
import com.emex.app.shared.data.Orders;
import com.emex.app.shared.data.OrdersExample;
import com.emex.app.shared.persistance.JuridicalPersonMapper;
import com.emex.app.shared.persistance.OrdersMapper;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Sergey Karavaev
 */
@Service
@Transactional
public class JuridicalPersonService {
    
    @Autowired
    private JuridicalPersonMapper juridicalPersonMapper;
    @Autowired
    private OrdersMapper ordersMapper;

    public int deleteJuridicalPersonByPrimaryKey(Integer id) {
        OrdersExample example = new OrdersExample();
        example.createCriteria().andJurClienIdEqualTo(id);
        List<Orders> orders = ordersMapper.selectByExample(example);
        if (null != orders && !orders.isEmpty()) {
            for (Orders ord: orders) {
                ordersMapper.deleteByPrimaryKey(ord.getId());
            }
        }
        return juridicalPersonMapper.deleteByPrimaryKey(id);
    }

    public JuridicalPerson insertJuridicalPerson(JuridicalPerson record) {
        juridicalPersonMapper.insert(record);
        return record;
    }

    public List<JuridicalPerson> selectByExample(JuridicalPersonExample example) {
        return juridicalPersonMapper.selectByExample(example);
    }

    public JuridicalPerson selectJuridicalPersonByPrimaryKey(Integer id) {
        return juridicalPersonMapper.selectByPrimaryKey(id);
    }

    public JuridicalPerson updateJuridicalPerson(JuridicalPerson record) {
        juridicalPersonMapper.updateByPrimaryKey(record);
        return record;
    }
    
    public List<JuridicalPerson> getAllJuridicalPersons() {
        return selectByExample(null);
    }
    
    public List<JuridicalPerson> getAllJuridicalPeronsByName(String name) {
        JuridicalPersonExample example = new JuridicalPersonExample();
        example.createCriteria().andNameLike("%" + name + "%");
        List<JuridicalPerson> juridicalPersons = selectByExample(example);
        return juridicalPersons;
    }
}
